/*     */ package dm.jdbc.dbaccess;
/*     */ 
/*     */ import java.math.BigInteger;
/*     */ import java.security.InvalidAlgorithmParameterException;
/*     */ import java.security.KeyPair;
/*     */ import java.security.KeyPairGenerator;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.security.PrivateKey;
/*     */ import java.sql.SQLException;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.interfaces.DHPrivateKey;
/*     */ import javax.crypto.spec.DHParameterSpec;
/*     */ import javax.crypto.spec.IvParameterSpec;
/*     */ import javax.crypto.spec.SecretKeySpec;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class MsgSecurity
/*     */ {
/*  22 */   private static final byte[] DEFAULT_IV = { 32, 33, 35, 36, 37, 
/*  23 */     38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 
/*  24 */     49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
/*  25 */     60, 61, 62, 63, 32 };
/*     */   
/*     */ 
/*     */   private static final String DHParaP = "C009D877BAF5FAF416B7F778E6115DCB90D65217DCC2F08A9DFCB5A192C593EBAB02929266B8DBFC2021039FDBD4B7FDE2B996E00008F57AE6EFB4ED3F17B6D3";
/*     */   
/*     */ 
/*     */   private static final String DHParaG = "5";
/*     */   
/*  33 */   private static DHParameterSpec DHParaSpec = null;
/*     */   
/*  35 */   private static KeyPairGenerator keyPairGenerator = null;
/*     */   
/*  37 */   private static BigInteger p = null;
/*     */   
/*  39 */   private static BigInteger g = null;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static IvParameterSpec getIV(int ivLength)
/*     */   {
/*  50 */     if (-1 == ivLength) {
/*  51 */       return new IvParameterSpec(new byte[0]);
/*     */     }
/*     */     
/*  54 */     byte[] iv = new byte[ivLength];
/*     */     
/*  56 */     System.arraycopy(DEFAULT_IV, 0, iv, 0, ivLength);
/*     */     
/*  58 */     IvParameterSpec algParams = new IvParameterSpec(iv);
/*     */     
/*  60 */     return algParams;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static BigInteger bytes2Bn(byte[] bnBytesSrc)
/*     */   {
/*  67 */     byte[] bnBytesTo = null;
/*     */     
/*  69 */     if (bnBytesSrc == null) {
/*  70 */       return null;
/*     */     }
/*     */     
/*  73 */     if (bnBytesSrc[0] == 0) {
/*  74 */       return new BigInteger(bnBytesSrc);
/*     */     }
/*     */     
/*  77 */     int validBytesCount = bnBytesSrc.length + 1;
/*  78 */     bnBytesTo = new byte[validBytesCount];
/*  79 */     bnBytesTo[0] = 0;
/*  80 */     System.arraycopy(bnBytesSrc, 0, bnBytesTo, 1, validBytesCount - 1);
/*     */     
/*  82 */     return new BigInteger(bnBytesTo);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   protected static byte[] bn2Bytes(BigInteger bn, int bnLen)
/*     */   {
/*  89 */     byte[] bnBytesSrc = null;
/*  90 */     byte[] bnBytesTemp = null;
/*  91 */     byte[] bnBytesTo = null;
/*  92 */     int validBytesCount = 0;
/*     */     
/*  94 */     if (bn == null) {
/*  95 */       return null;
/*     */     }
/*     */     
/*  98 */     bnBytesSrc = bn.toByteArray();
/*     */     
/*     */ 
/* 101 */     if (bnBytesSrc[0] != 0) {
/* 102 */       bnBytesTemp = bnBytesSrc;
/* 103 */       validBytesCount = bnBytesTemp.length;
/*     */     } else {
/* 105 */       validBytesCount = bnBytesSrc.length - 1;
/* 106 */       bnBytesTemp = new byte[validBytesCount];
/* 107 */       System.arraycopy(bnBytesSrc, 1, bnBytesTemp, 0, validBytesCount);
/*     */     }
/*     */     int leading_zero_count;
/*     */     int leading_zero_count;
/* 111 */     if (bnLen == 0) {
/* 112 */       leading_zero_count = 0;
/*     */     } else {
/* 114 */       leading_zero_count = bnLen - validBytesCount;
/*     */     }
/*     */     
/*     */ 
/* 118 */     if (leading_zero_count > 0) {
/* 119 */       int i = 0;
/* 120 */       bnBytesTo = new byte[64];
/* 121 */       for (i = 0; i < leading_zero_count; i++) {
/* 122 */         bnBytesTo[i] = 0;
/*     */       }
/*     */       
/* 125 */       System.arraycopy(bnBytesTemp, 0, bnBytesTo, i, validBytesCount);
/*     */     } else {
/* 127 */       bnBytesTo = bnBytesTemp;
/*     */     }
/*     */     
/* 130 */     return bnBytesTo;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static synchronized KeyPair newClientKeyPair()
/*     */     throws SQLException
/*     */   {
/* 142 */     if (keyPairGenerator == null) {
/* 143 */       p = new BigInteger("C009D877BAF5FAF416B7F778E6115DCB90D65217DCC2F08A9DFCB5A192C593EBAB02929266B8DBFC2021039FDBD4B7FDE2B996E00008F57AE6EFB4ED3F17B6D3", 16);
/* 144 */       g = new BigInteger("5", 16);
/* 145 */       DHParaSpec = new DHParameterSpec(p, g, 512);
/*     */       
/*     */ 
/*     */       try
/*     */       {
/* 150 */         keyPairGenerator = KeyPairGenerator.getInstance("DH");
/*     */         
/*     */ 
/*     */ 
/* 154 */         keyPairGenerator.initialize(DHParaSpec);
/*     */       } catch (NoSuchAlgorithmException e) {
/* 156 */         keyPairGenerator = null;
/* 157 */         DBError.throwSQLException("can't find sunjce_provider.jar");
/*     */       } catch (InvalidAlgorithmParameterException e) {
/* 159 */         keyPairGenerator = null;
/* 160 */         DBError.throwSQLException("can't find sunjce_provider.jar");
/*     */       }
/*     */     }
/*     */     
/* 164 */     if (keyPairGenerator == null)
/*     */     {
/*     */ 
/* 167 */       return null;
/*     */     }
/*     */     
/* 170 */     KeyPair clientKeyPair = keyPairGenerator.generateKeyPair();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 175 */     return clientKeyPair;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static byte[] computeSessionKey(PrivateKey clientPrivKey, byte[] serverPubKey)
/*     */   {
/* 183 */     BigInteger serverKeyX = bytes2Bn(serverPubKey);
/*     */     
/* 185 */     BigInteger clientPrivKeyX = ((DHPrivateKey)clientPrivKey).getX();
/*     */     
/* 187 */     BigInteger sessinoKeyBN = serverKeyX.modPow(clientPrivKeyX, p);
/*     */     
/* 189 */     return bn2Bytes(sessinoKeyBN, 0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static Cipher newCipher(int cipherMode, SymmCipherDesc cipherDesc, byte[] sessionKey)
/*     */     throws Exception
/*     */   {
/* 202 */     Cipher encrypt_cipher = null;
/*     */     
/* 204 */     encrypt_cipher = Cipher.getInstance(cipherDesc.getAlgorithmFullName());
/*     */     
/* 206 */     SecretKeySpec keySpec = new SecretKeySpec(sessionKey, 
/* 207 */       cipherDesc.getAlgorithmName());
/*     */     
/* 209 */     if ((1 == cipherDesc.getWork_mode()) || 
/* 210 */       (4096 == cipherDesc.getAlgorithmType())) {
/* 211 */       encrypt_cipher.init(cipherMode, keySpec);
/*     */     } else {
/* 213 */       encrypt_cipher.init(cipherMode, keySpec, 
/* 214 */         getIV(cipherDesc.getIvLength()));
/*     */     }
/*     */     
/* 217 */     return encrypt_cipher;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\dbaccess\MsgSecurity.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */